<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  header("location:checklogin.php");
}

////// select user and from and to id :start
$fromId              = (isset($_REQUEST['fromId'])) ? $_REQUEST['fromId'] : 0;
$loadingInvoiceId    = 0;
$invoiceNo           = (isset($_REQUEST['invoiceNo'])) ? $_REQUEST['invoiceNo'] : 0 ;
$octroi              = (isset($_REQUEST['octroi'])) ? $_REQUEST['octroi'] : 0 ;
$octroiNo            = (isset($_REQUEST['octroiNo'])) ? $_REQUEST['octroiNo'] : 0 ;
$remarks             = (isset($_REQUEST['remarks'])) ? $_REQUEST['remarks'] :  "";
$unloadDate          = date('Y-m-d');
if(isset($_REQUEST['unloadStatus']))
{
	foreach($_REQUEST['unloadStatus'] as $key => $value)
	{
		if($_REQUEST['unloadStatus'][$key] == 'on')
		{
	    $updateUnload = "UPDATE loadinginvoicedetail 
	                        SET octroi = ".$_REQUEST['octroi'][$key].",
	                            octroiNo = '".$_REQUEST['octroiNo'][$key]."',
	                            remarks  = '".$_REQUEST['remarks'][$key]."',
	                            unloadStatus  = 'Y'
	                      WHERE loadingInvoiceDetailId = ".$key;
	    $updateUnloadRes = mysql_query($updateUnload);
	    if(!$updateUnloadRes)
	    {
	      echo "UDATE FAIL";
	    }
	    else
	    {
	      header("Location:unloadingregister.php");
	    }
	  }
	}
  $updateUnloadDate = "UPDATE loadinginvoice SET unloadingDate = '".$unloadDate."'
	                     WHERE invoiceNo = ".$_REQUEST['invoiceNo']."";
	$updateUnloadDateRes =mysql_query($updateUnloadDate);
	if(!$updateUnloadDateRes)
	{
		echo "Upadte Unloading Date Fail";
	}
}
////// select user and from and to id :end

$truckId               = 0;
$toId                  = 0;
$invoiceDate           = 0;
$unloadingInvoiceNo    = 0;
$toBranchName          = "";
$truckNo               = "";
$dataNotFound          = 0;
$toId                  = (isset($_POST['toId'])) ? $_POST['toId'] : 0;
$selectLoadInvoice = "SELECT loadingInvoiceId,truckNo,toId,invoiceDate,invoiceNo,fromId,toBranch.branchName AS toBranchName,toId 
                        FROM loadinginvoice
                        LEFT JOIN branch AS toBranch ON toBranch.branchId = loadinginvoice.toId 
                        LEFT JOIN truckmaster ON truckmaster.truckId = loadinginvoice.truckId 
                       WHERE toId = ".$_SESSION['ses_branchId']."
                         AND invoiceNo = ".$_REQUEST['invoiceNo']."";
$selectLoadInvoiceRes = mysql_query($selectLoadInvoice);
while($loadRow = mysql_fetch_array($selectLoadInvoiceRes))
{
	$loadingInvoiceId = $loadRow['loadingInvoiceId'];
	$truckNo          = $loadRow['truckNo'];
	$toBranchName     = $loadRow['toBranchName'];
	$invoiceDate      = $loadRow['invoiceDate'];
}

if($loadingInvoiceId == 0)
{
	$dataNotFound = 1;
}

$t=0;
$loadInvoiceArray = array();
$selectLoadInvoice = "SELECT loadingInvoiceDetailId,lrNo,consignor,consignee,weight,discription,toPay,billType,unloadStatus
                        FROM loadinginvoicedetail
                       WHERE loadingInvoiceId = ".$loadingInvoiceId."
                       AND unloadStatus = 'N'";
$selectLoadInvoiceRes = mysql_query($selectLoadInvoice);
while($loadInvoiceRow = mysql_fetch_array($selectLoadInvoiceRes))
{
	$loadInvoiceArray[$t]['loadingInvoiceDetailId']  = $loadInvoiceRow['loadingInvoiceDetailId'];
	$loadInvoiceArray[$t]['lrNo']                    = $loadInvoiceRow['lrNo'];
	$loadInvoiceArray[$t]['consignor']               = $loadInvoiceRow['consignor'];
	$loadInvoiceArray[$t]['consignee']               = $loadInvoiceRow['consignee'];
	$loadInvoiceArray[$t]['weight']                  = $loadInvoiceRow['weight'];
	$loadInvoiceArray[$t]['discription']             = $loadInvoiceRow['discription'];
	$loadInvoiceArray[$t]['toPay']                   = $loadInvoiceRow['toPay'];
	$loadInvoiceArray[$t]['billType']                = $loadInvoiceRow['billType'];
	$t++;
}

$revicedArrayValue[0]  = "Yes";
$revicedArrayOutput[0] = "Yes";
$revicedArrayValue[1]  = "No";
$revicedArrayOutput[1] = "No";

$selectUnload = "SELECT MAX(unloadingInvoiceNo) + 1 AS unloadingInvoiceNo
                   FROM loadinginvoice
                  WHERE loadinginvoice.toId =".$_SESSION['ses_branchId'];
$selectUnloadRes = mysql_query($selectUnload);
if($unloadSelect = mysql_fetch_array($selectUnloadRes))
{
  $unloadingInvoiceNo = $unloadSelect['unloadingInvoiceNo'];
}
$selectBrach = "SELECT loadingInvoiceId,toId,invoiceNo
                  FROM loadinginvoice
                 WHERE fromId = ".$_SESSION['ses_branchId']."
                   AND invoiceNo = ".$invoiceNo."";
$selectBrachRes = mysql_query($selectBrach);
if($branchRow = mysql_fetch_array($selectBrachRes))
{
	$toId = $branchRow['toId'];
}
include("./bottom.php");
$smarty->assign("toBranchName",$toBranchName);
$smarty->assign("revicedArrayOutput",$revicedArrayOutput);
$smarty->assign("revicedArrayValue",$revicedArrayValue);
$smarty->assign("loadInvoiceArray",$loadInvoiceArray);
$smarty->assign("loadingInvoiceId",$loadingInvoiceId);
$smarty->assign("unloadingInvoiceNo",$unloadingInvoiceNo);
$smarty->assign("truckNo",$truckNo);
$smarty->assign("toId",$toId);
$smarty->assign("invoiceDate",$invoiceDate);
$smarty->assign("invoiceNo",$invoiceNo);
$smarty->assign("dataNotFound",$dataNotFound);
if($_SESSION['ses_branchId'] == $toId)
{
	echo "You Cant Unload This Invoice"; 
}
else 
{
  $smarty->display('setLoadingData.tpl'); 
}
?>